import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yinsh
 * Date: 2025-01-21
 * Time: 20:55
 */
class MyStack {
    Queue<Integer> queue1 = new LinkedList<>();
    Queue<Integer> queue2 = new LinkedList<>();

    public MyStack() {

    }

    public void push(int x) {
        if (empty()) {
            queue1.add(x);
        }else {
            if (queue1.isEmpty()) {
                queue2.add(x);
            } else if (queue2.isEmpty()) {
                queue1.add(x);
            }
        }
    }

    public int pop() {
        int size = -1;
        if (empty()) {
            return -1;
        }else {
            if (queue1.isEmpty()) {
                size = queue2.size();
                for (int i = 1; i < size ; i++) {
                    queue1.add(queue2.poll());
                }
                return queue2.poll();
            }else {
                size = queue1.size();
                for (int i = 1; i < size; i++) {
                    queue2.add(queue1.poll());
                }
                return queue1.poll();
            }
        }
    }

    public int top() {
        int size = -1;
        int tamp = -1;
        if (empty()) {
            return -1;
        }else {
            if (queue1.isEmpty()) {
                size = queue2.size();
                for (int i = 0; i < size ; i++) {
                    tamp = queue2.peek();
                    queue1.add(queue2.poll());
                }
                return tamp;
            }else {
                size = queue1.size();
                for (int i = 0; i < size; i++) {
                    tamp = queue1.peek();
                    queue2.add(queue1.poll());
                }
                return tamp;
            }
        }
    }

    public boolean empty() {
        if (queue1.isEmpty()&&queue2.isEmpty()) {
            return true;
        }
        return false;
    }
}
